package com.example.ecommerce.controller;

import com.example.ecommerce.common.exception.LoginException;
import com.example.ecommerce.dto.LoginRequestDTO;
import com.example.ecommerce.dto.LoginResponseDTO;
import com.example.ecommerce.dto.UserDTO;
import com.example.ecommerce.service.AuthService;
import com.example.ecommerce.service.UserService;
import com.example.ecommerce.util.ActionResult;
import io.swagger.annotations.Api;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;

@Api(tags = "认证管理")
@RestController
@RequestMapping("/api/auth")
@CrossOrigin(origins = "*", maxAge = 3600)
@Controller
public class AuthController {

    @Autowired
    private UserService userService;
//
//    @ApiOperation("用户登录")
//    @PostMapping("/login")
//    public ApiResponse<LoginResponse> login(@Valid @RequestBody LoginRequest request) {
////        LoginResponse response = userService.login(request);
//        return ApiResponse.success(null);
//    };
    @Autowired
    private AuthService authService;

    @PostMapping("/login")
    public ActionResult<LoginResponseDTO> login(@Valid @RequestBody LoginRequestDTO loginRequest) throws LoginException {
        return  ActionResult.success(authService.login(loginRequest)) ;
    }

    @PostMapping("/refresh")
    public String refreshToken(@RequestParam String refreshToken) {
        return authService.refreshToken(refreshToken);
    }

    @PostMapping("/logout")
    public ActionResult logout() {
        authService.logout();
        return ActionResult.success();
    }

    @GetMapping("/current")
    public ActionResult<UserDTO> getCurrentUser() {
        return ActionResult.success(userService.getCurrentUser());
    }
}